home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n5.arc / LIST1.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  1KB  |  50 lines

  1. /*---------------------------------------- Listing 1 ------
  2.  * Demonstration of atexit().
  3.  * by Thomas Siering
  4.  *
  5.  * (c) 1991 C Gazette. Object code may be freely used,
  6.  * source code may be used if authorship and publication
  7.  * are acknowledged.
  8.  *-------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. void ReleaseFile ( void );
  14. void FreeMemory  ( void );
  15. void main        ( void );
  16.  
  17.  
  18. FILE *File;
  19. void *Memory;
  20.  
  21. void main()
  22. {
  23.     /* Open a file and provide for its closing 
  24.        at ANY subsequent time.  */
  25.  
  26.     if (( File = fopen ( "FILE.DAT", "w" )) == NULL )
  27.         return;
  28.     if ( atexit ( ReleaseFile ) != 0 )  /* = 0 if installed OK */
  29.         return;
  30.  
  31.     /* Do likewise for memory */
  32.     if (( Memory = malloc ( 10 )) == NULL )
  33.         return;
  34.     if ( atexit ( FreeMemory ) != 0 )
  35.         return;
  36. }
  37.  
  38. void ReleaseFile()
  39. {
  40.     if ( fclose ( File ) != 0 )
  41.         fprintf ( stderr, "File FILE.DAT not closed\n" );
  42.     else
  43.         fprintf ( stdout, "File FILE.DAT has been closed\n" );
  44. }
  45.  
  46. void FreeMemory()
  47. {
  48.     free ( Memory );
  49.     fprintf ( stdout, "Memory freed\n" );
  50. }